package com.xuan.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuan.mapper.UserMapper;
import com.xuan.model.entity.User;
import com.xuan.model.respons.ResultBody;
import com.xuan.service.IUserService;
import com.xuan.utils.JudgeOb;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Objects;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Axuan
 * @since 2021-07-09
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public String login(String username) {

        User user = userMapper.selectById(username);
        if (user != null){
            return user.getPwd();
        }
        return null;
    }

    @Override
    public boolean register(User user) {
        if (JudgeOb.isNotEmptyBean(user)){//判断是否不为空
            //进行加密处理
            user.setPwd(new BCryptPasswordEncoder().encode(user.getPwd()));
            return this.save(user);
        }
        return false;
    }


    @Override
    public ResultBody chekUsername(String username) {
        User user = userMapper.selectById(username);
        log.info("user===" + user);
        if (Objects.isNull(user)){
            return new ResultBody().OK("该用户名可用");
        }else {
           return new ResultBody().Fail("该用户名不可用");
        }
    }

    @Override
    public ResultBody savaUser(User user) {

        user.setPwd(new BCryptPasswordEncoder(10).encode(user.getPwd()));

        return this.save(user) ? new ResultBody().OK("注册成功") : new ResultBody().Fail("注册失败");
    }

    @Override
    public ResultBody deleteByUsername(String username) {
        if (StringUtils.isNotBlank(username)){
            boolean b = this.removeById(username);
            return b ? new ResultBody().OK("删除成功") : new ResultBody().Fail("删除失败");
        }
        return new ResultBody().Fail("删除失败");
    }

    @Override
    public ResultBody resetPassword(User user) {
        log.info("收到用户信息为======"+user.toString());
        if (JudgeOb.isNotEmptyBean(user)){
            User user1 = userMapper.selectById(user.getUsername());
            if (!Objects.isNull(user1) && JudgeOb.isNotEmptyBean(user1)){
                if (user.getMobile().equals(user1.getMobile())){
                    user.setPwd(new BCryptPasswordEncoder(10).encode(user.getPwd()));
                    int i = userMapper.updateById(user);
                    return i > 0? new ResultBody().OK() : new ResultBody().Fail("修改失败");
                }else {
                    return new ResultBody().Fail("用户名与手机不符");
                }
            }else{
                return new ResultBody().Fail("用户尚未注册");
            }
        }
        return new ResultBody().Fail("信息不正确");
    }

}
